var express = require('express');
var router = express.Router();
var r = require('../biz/users')

/* GET users listing. */
router.get('/login', function(req, res, next) {
  r.login(req,res)
});

/* POST users listing. */
router.post('/regist', function(req, res, next) {
    r.regist(req,res)
});

router.get('/getUserInfo', function(req, res, next) {
    r.getUserInfo(req,res)
});

var svgCaptcha = require('svg-captcha');
router.get('/getCaptcha', function (req,res,next) {
    console.log('当前用户为：'+JSON.stringify(req.headers.session))
    var captcha = svgCaptcha.create({
        //翻转颜色
        inverse: false,
        //字体大小
        fontSize: 36,
        //噪声线条数
        noise: 0,
        //宽度
        width: 80,
        //高度
        height: 30
    });
    //保存到session，忽略大小写
    req.session.captcha = captcha.text.toLowerCase();
    console.log(req.session.captcha);
    res.setHeader('Content-Type','image/svg+xml');
    res.write(String(captcha.data));
    res.end();
})

router.get('/captchaValid',(req,resp)=>{
    let captcha = req.query.captcha;
    console.log(captcha)
    if(captcha == req.session.captcha) {
        resp.send()
    } else {
        throw new Error('验证失败')
    }
})

router.get('/get', function(req, res, next) {
    res.send(req.session.name)
});
router.get('/getglobal', function(req, res, next) {
    res.send(global.globalname)
});
router.post('/add', function(req, res, next) {
    req.session.name = req.body.name
    res.end()
});

module.exports = router;
